﻿package game 
{
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import game.events.EventData;

	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import game.exchangeddata.HashTable;
	/**
	 * ...
	 * @author Doan Nguyen Thuy Nha Truc (Bamboo)	
	 */
	public class ChannelPanel extends MovieClip
	{
		public var txtChannelTf:TextField;
		public var channelActiving:String = "";
		public var channelActived:String = "";
		public var whiteColor:uint = 0xFFFFFF;
		public var yellowColor:uint = 0xFFCC32;
		public function ChannelPanel() 
		{
			
		}
		
		public function initText(hashTableInfo:HashTable):void {
			txtChannelTf.text = hashTableInfo.lookupString("channel");
		}
		
		public function addListChannel(data:Array):void {
			var txtFormat:TextFormat = new TextFormat();
			txtFormat.color = whiteColor;
			txtFormat.font = "Tahoma";
			txtFormat.size = 14;
			txtFormat.bold = true;
			for (var i:int = 0; i < data.length; i++) {
				
				var cnIcon:MovieClip = MovieClip(new ChannelIcon());
				
				if(i==0){
					cnIcon.x = txtChannelTf.x + txtChannelTf.textWidth + 20;
					//trace("cnIcon.x:" + cnIcon.x);
				}else {
					var lastChildTf:MovieClip = MovieClip(this.getChildByName(data[i - 1]['fms']));
					cnIcon.x = lastChildTf.x + lastChildTf.width;
				}
				cnIcon.name = data[i]['fms'];
				cnIcon.txtTf.text = data[i]['id'];
				//trace("data[i]['id']" + data[i]['id']) ;
				cnIcon.txtTf.setTextFormat(txtFormat);
				cnIcon.y = txtChannelTf.y;
				cnIcon.width = cnIcon.txtTf.textWidth + 20;
				cnIcon.height = 22;
				cnIcon.txtTf.mouseEnabled = false;
				cnIcon.buttonMode = true;
				cnIcon.addEventListener(MouseEvent.CLICK, clickHdl);
				addChild(cnIcon);
			}
		}
		
		private function clickHdl(e:Event):void 
		{
			if (channelActiving != e.currentTarget.name)
			{
				channelActived = channelActiving;
				channelActiving = e.currentTarget.name;
				//trace("channelActived:" + channelActived + "channelActiving:" + channelActiving);
				var temp:TextField;
				var txtFormat:TextFormat;
				if (channelActived != "") {
					setChannelActiving(channelActived, whiteColor);
					
				}
				setChannelActiving(channelActiving, yellowColor);
				GlobalVariables.fmsChannel = e.currentTarget.name;
				var event:EventData = new EventData(EventData.UPDATE_VIDEO, true);
				event.ms = e.currentTarget.name;
				dispatchEvent(event);
			}
		}
		/**
		 * 
		 * @param	str: name MovieClip that change color
		 * @param	color
		 */
		public function setChannelActiving(str:String, color:uint):void {
		
			var temp:MovieClip;
			var txtFormat:TextFormat;
			temp = MovieClip(this.getChildByName(str));
			
			txtFormat = temp.txtTf.getTextFormat();
			txtFormat.color = color;
			temp.txtTf.setTextFormat(txtFormat);
		}
		
		public function updateChannelActiving(str:String):void {
			channelActiving = str;
		}
	}

}